"use strict";
var prefix = "/report/legalPerson";
var legalPersonId = $("#legalPersonId").val();
$(function(){
	init(legalPersonId);
})

/**
 * 查询当前法人信用报告
 * @param legalPersonId 法人ID
 * @returns
 */
function init(legalPersonId){
	$.ajax({
		data : {
			legalPersonId : legalPersonId
		}, 
		url : prefix + '/detailNumber' ,
		success : function(r) {
			//console.log(r);
			var companyName = r.obj.legalPersonReportBaseVO.companyName;
			$(".textblue30").html(companyName);
			
			var detailMap = r.obj.detailMap;
			var detailTab  = "";	// 导航栏
			var detail  = "";	// 详细信息
			var i = 0;
			$.each(detailMap,function(k,v){
				detailTab += "<dd "+ (i == 0? "class=\"fli\"":"") +">" + k + " <span>" +v.total + "</span>" + "</dd>\n\r";
				detail += "<div " + (i == 0? "class=\"fdiv div\"":"class=\"div\"") +">\n\r";
				detail += "	<div class=\"panel-body\">";
				$.each(v.list,function(tK,tV){
				detail += "		<a " + (tV.length > 0 ? "href=\"#"+ tK +"\"":"class=\"disable\"" ) +">" + tK +" " + ( tV.length > 0 && tV[0].link_way == 0?tV[0].busi_value:"") +"</a>";
				});
				detail += "	</div>";
				detail += "	<ul>\n\r";
				detail += "		<li class=\"companynewstb\">\n\r";
				$.each(v.list,function(k2,v2){
				detail += "			<p class=\"title\" id=\""+ k2 +"\">\n\r";
				detail += "				<i class=\"iblock\"></i>\n\r";
				detail += "				<span class=\"texttitle\">"+ k2 +"</span>\n\r";
				detail += "			</p>\n\r";
				detail += "			<table class=\"pub-table\">\n\r";
					if(v2.length <= 0){
				detail += "				<tr>\n\r";
				detail += "					<th colspan=\"2\" style=\"text-align: left;\">无记录！</th>\n\r";
				detail += "				</tr>\n\r";
					}else{
						$.each(v2,function(k3,v3){
				detail += "				<tr>\n\r";
				detail += "					<th>" + v3.busi_name + "</th>\n\r";
				
						switch(v3.link_way)
						{
						case 0:
				detail += "					<td><a href=\"javaScript:void(0)\" onclick=\"queryDetail('"+ legalPersonId +"','"+ v3.busi_id +"',this)\">" + (v3.busi_value != null?v3.busi_value:"") + "</a></td>\n\r";
						break;
						case 4:
				detail += "					<td><a href=\"javaScript:void(0)\" onclick=\"queryQiChacha('"+ companyName +"',"+ v3.busi_id +",this)\">查看详情</a></td>\n\r";
						break;
						default:
				detail += "					<td>" + (v3.busi_value != null?v3.busi_value:"") + "</td>\n\r";			
						}
						
				detail += "				</tr>\n\r";
						})
					}
				detail += "			</table>\n\r";
				});
				detail += "		</li>\n\r";
				detail += "	</ul>\n\r";
				detail += "</div>\n\r";
				
				i++;
			})
			$("#companydetailtab").html(detailTab);
			$("#companydetail").html(detail);
			tab();
		}
	})
}

/**
 * 查询明细
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryDetail(legalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				legalPersonId : legalPersonId,
				busiId : busiId
			}, 
			url : prefix + '/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/legalPerson/viewDetail/" + r.tr[0].legal_person_id)
				}else{
					var Str  = "<tr>\n\r";
						Str += "	<td colspan=\"2\">\n\r";
						Str += "		<table class=\"detail-message\">\n\r";
						Str += "			<tbody>\n\r";
						Str += "				<tr>\n\r";
						$.each(r.th,function(key1,value1){
						Str += "					<th>"+ value1 +"</th>\n\r";
						})
						Str += "				</tr>\n\r";
						$.each(r.tr,function(key2,value2){;
						Str += "				<tr>\n\r";
							$.each(value2,function(key3,value3){
						Str += "					<td>"+ value3 +"</td>\n\r";
							})
						Str += "				</tr>\n\r";
						})
						Str += "			</tbody>\n\r";
						Str += "		</table>\n\r";
						Str += "	</td>\n\r";
						Str += "</tr>\n\r";			
					$(_this).parents("tr").after(Str);
				}
			}
		})
	}
}

function tab(){
	var tabs = document.getElementById("companydetailtab").getElementsByTagName("dd");
	var divs = document.getElementById("companydetail").getElementsByClassName("div");

	for (var i = 0; i < tabs.length; i++) {
		tabs[i].onclick = function() {
			change(this);
		}
	}

	function change(obj) {
		for (var i = 0; i < tabs.length; i++) {
			if (tabs[i] == obj) {
				tabs[i].className = "fli";
				divs[i].className = "fdiv div";
			} else {
				tabs[i].className = "";
				divs[i].className = "div";
			}
		}
	}
}

/**
 * 跳转到打印报告预览页
 * @returns
 */
function printReport(){
	  window.open("/reportPDF/downloadPDF/"+ legalPersonId + "/"+$("#timestamp").val());
}

/**
 * 企查查接口
 * @param qymc 企业名称
 * @param busiId 指标ID
 * @param _this 当前元素节点
 * @returns
 */
function queryQiChacha(companyName,busiId,_this){
	if($(_this).parents("tr").next().find("#qichacha-message").length != 0){
		$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			url: prefix + "/queryQiChacha",
			data: {
				companyName : companyName,
				busiId : busiId
			},
			success: function(r){
				var result = r.result;
				var Str  = "<tr>\n\r";
				  	  Str += "	<td id=\"qichacha-message\" colspan=\"2\">\n\r";
				if(result != null){
					var key2Text =new Object; // 中文名对应
					for(var i in r.name){
						key2Text[i] = r.name[i];
					}
					var a = fixResult2(result,key2Text,"");
						  Str += a;
				}else{
					Str += "暂无数据!";
				}
				  Str += "	</td>\n\r";
				  Str += "</tr>\n\r";
				  $(_this).parents("tr").after(Str);
			}
		})
	}
}

/**
 * 递归处理企查查数据
 * @param data 原始数据
 * @param key2Text 键名对应
 * @param _Str 拼接文本
 * @returns
 */
function fixResult2(data, key2Text, _Str) {
	var Str = _Str;
	if ($.isArray(data)) { // 如果传入对象是Array，则进行递归
		$.each(data, function(k, v) {
			Str = fixResult2(v, key2Text, Str);
		})
	} else if ($.isPlainObject(data)) { // 如果是Object，则进行遍历
		Str += "<table class=\"pub-table detail-message\">";
		$.each(data, function(k, v) {
			if ($.isPlainObject(v)) { // 如果是Object，则递归遍历
				if(key2Text[k] != undefined){
					Str += "<tr><th>" + key2Text[k] + "</th><td>";
					Str = fixResult2(v, key2Text, Str);
					Str += "</td></tr>";
				}
			} else if ($.isArray(v)) { // 如果是Array,则进行递归
				$.each(v, function(k2, v2) {
					Str = fixResult2(v2, key2Text, Str);
				})
			} else {
				if(key2Text[k] != undefined){
					Str += "<tr><th>" + key2Text[k] + "</th><td>" + v
							+ "</td></tr>"
				}
			}
		})
		Str += "</table>";
	}
	return Str;
}
